import { fail } from 'assert';
import config from '@/config/config.js';


const request = (option) => {
	return new Promise((resolve, reject) => {
		uni.request({
			url: config.apiBaseUrl + option.url,
			method: option.method || 'GET',
			header: option.header || {},
			data: option.data || {},
			success: (res) => {
				if (res.data.code == 200) {
					resolve(res.data)
				} else {
					let errInfo = res.data?.message || '未知错误'
					console.log(errInfo);
					uni.showToast({
						title: errInfo,
						duration: 3000
					})
					
					setTimeout(function() {
						reject(new Error(res.statusCode))
					}, 3000);
					
				}
			},

			fail: (err) => {
				let errorInfo = '';
				if (err.errMsg.includes('timeout')) {
					errorInfo = '请求超时，请稍后重试'
				} else if (err.errMsg.includes('abort')) {
					errorInfo = '请求数据错误，请稍后重试'
				} else {
					errorInfo = '网络错误'
				}
				uni.showToast({
					title: errorInfo,
					duration: 5000,
				});
				reject(new Error(errorInfo))
			}
		});
	})
}

export default request;